<template>
  <el-dialog
    :visible.sync="visible"
    title="排房"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="40%"
    top="100px"
    :append-to-body="true"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '80px' : '80px'"
    >
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="到达时间" prop="arriveDate">
            <el-date-picker
              v-model="dataForm.arriveDate"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="到达时间"
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="离开时间" prop="leaveDate">
            <el-date-picker
              disabled
              v-model="dataForm.leaveDate"
              type="date"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="离开时间"
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房型" prop="houseBaseId">
            <el-select
              v-model="dataForm.houseBaseId"
              placeholder="请选择"
              style="width: 100%"
              clearable
            >
              <el-option
                v-for="(item, index) in houseBases"
                :key="index"
                :label="item.name"
                :value="item.houseBaseId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="房间号" prop="roomFloor">
            <el-select
              v-model="roomNumbers"
              placeholder="请选择"
              style="width: 100%"
              clearable
              multiple
            >
              <el-option
                v-for="(item, index) in rooms"
                :key="index"
                :label="item.roomNumber"
                :value="item.roomNumber"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import editorVue from "@/components/editor";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        arriveDate: "",
        houseBaseId: "",
        leaveDate: "",
        orderId: "",
        roomFloor: [],
      },
      roomNumbers: [],
      houseBases: [],
      rooms: [],
    };
  },
  components: {
    editorVue,
  },
  props: ["orderId", "num"],
  computed: {
    dataRule() {
      return {
        arriveDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        leaveDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        houseBaseId: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
        roomFloor: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "change",
          },
        ],
      };
    },
  },
  watch: {
    "dataForm.houseBaseId"(newval, oldval) {
      if (newval && newval != oldval) {
        let obj = this.houseBases.filter((item) => item.houseBaseId == newval);
        if (obj[0]) {
          this.roomNumbers = [];
          this.rooms = obj[0].roomFloor;
        }
      }
    },
    "dataForm.arriveDate"(newval, oldval) {
      if (newval != oldval) {
        this.dataForm.leaveDate = this.GetDateStr(this.num);
      }
    },
    roomNumbers(newval, oldval) {
      if (newval != oldval) {
         this.dataForm.roomFloor =newval.map((i) => {
          return this.rooms.filter((item) => item.roomNumber == i)[0];
        });
      }
    },
  },
  methods: {
    GetDateStr(AddCount) {
      var dd = new Date(this.dataForm.arriveDate);
      dd.setDate(dd.getDate() + AddCount); //获取AddDayCount天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1; //获取当前月份的日期
      var d = dd.getDate();
      m = m > 9 ? m : "0" + m;
      d = d > 9 ? d : "0" + d;
      return y + "-" + m + "-" + d;
    },
    // 房型列表
    gethouseBases() {
      this.$http
        .get(
          `/hotel/house/base/getHouseSearchBox/${this.$store.state.user.homestayId}`
        )
        .then((res) => {
          this.houseBases = res.data.data;
        });
    },
    init() {
      this.visible = true;
      this.dataForm.orderId = this.dataForm.id;
      this.gethouseBases();
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();

        // if (this.dataForm.id) {
        //   this.getInfo();
        // }
      });
    },
    //获取信息
    getInfo() {
      this.$http
        .get("/scenic/commnoticeo/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.$nextTick(() => {
            // this.$refs.infoIntroduceRef.init();
          });
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
       
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http
            .post("/hotel/guest/room/handleChangeDate", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
.txt {
  line-height: 40px;
  height: auto;
  margin: 0;
  padding: 0;

  &.content {
    ::v-deep p {
      padding: 0;
      line-height: 24px;
      margin: 0 !important;
    }

    ::v-deep img {
      width: 100%;
    }

    img {
      width: 100%;
    }
  }
}
</style>
